package util;

import java.util.Date;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

/**
 * Created by wl 0704.
 */
@Controller
@RequestMapping("/")
public class WeixinUtil {



	private static String WX_APPID = "wx1ca996b14c35ef0f";
	private static String WX_APPSECRET = "e384be821e7f2e0dd14818fb5e588429";
	// 采用http GET方式请求获得jsapi_ticket（有效期7200秒，开发者必须在自己的服务全局缓存jsapi_ticket）
	public final static String WX_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

	private static WxJsTicket apiTicket =(WxJsTicket)EhcacheUtil.getInstance().get("myCache", "apiTicket");;
   
	
	/**
	 * 获取jsApiTicket
	 * 
	 * @return
	 */
	@RequestMapping(value="/getwxconfig",produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public static JSONObject getWxConfigInfo(@RequestParam String url) {
		
		System.out.println(url);
		long now = new Date().getTime();
		JSONObject jsonObject = new JSONObject();
		// 第三方用户唯一凭证
		String appId = WX_APPID;
		// 第三方用户唯一凭证密钥
		String appSecret = WX_APPSECRET;
		String ticket = "";
		
		if (apiTicket != null) {
			if (now > apiTicket.getTicketStart() && now < apiTicket.getTicketEnd()) {
				ticket = apiTicket.getTicket();
			}
		} else {
			
			String at = GetToken.getAccess_token(appId, appSecret);
			EhcacheUtil.getInstance().put("myCache", "ACCESS_TOKEN", at);
		
			String requestUrl = WX_JSAPI_TICKET_URL.replace("ACCESS_TOKEN", at);
			jsonObject = GetToken.httpRequest(requestUrl, "GET", null);
			if (jsonObject != null && jsonObject.get("ticket") != null) {
				ticket = jsonObject.getString("ticket");
					
				EhcacheUtil.getInstance().put("myCache", "apiTicket",ticket);
			}
		}
		String nonceStr = getUuid();
		String timestamp = Long.toString(System.currentTimeMillis() / 1000);
		String signature = SignUtil.createSignature(ticket, nonceStr, timestamp, url);
		System.out.println("ticket:"+ ticket);
		System.out.println("nonceStr:"+ nonceStr);
		System.out.println("timestamp:"+ timestamp);
		System.out.println("signature:"+ signature);
		System.out.println("url:"+ url);
		jsonObject.put("jsapi_ticket", ticket);
		jsonObject.put("nonceStr", nonceStr);
		jsonObject.put("timestamp", timestamp);
		jsonObject.put("signature", signature);
		jsonObject.put("appId", appId);;

		
		System.out.println(jsonObject);
		return jsonObject;

	}

	public static String getUuid() {
		return java.util.UUID.randomUUID().toString().replaceAll("-", "");
	}
	/**
     * @CacheEvict 
     * value 缓存的名称，在 spring 配置文件中定义，必须指定至少一个
     * 相当于delete()缓存
     */
	@CachePut(value="myCache",key="#ticket")
    public WxJsTicket putTicket(WxJsTicket ticket)
    {
    	return ticket;
    }
	/**
     * @CacheEvict 
     * value 缓存的名称，在 spring 配置文件中定义，必须指定至少一个
     * 相当于delete()缓存
     */
	@CachePut(value="myCache",key="#token")
    public String putToken(String token)
    {
    	return token;
    }
	/**
     * @CacheEvict 
     * value 缓存的名称，在 spring 配置文件中定义，必须指定至少一个
     * 相当于delete()缓存
     */
	@Cacheable(value="myCache",key="#ticket")
    public WxJsTicket getTicket(WxJsTicket ticket)
    {
    	return ticket;
    }
	/**
     * @CacheEvict 
     * value 缓存的名称，在 spring 配置文件中定义，必须指定至少一个
     * 相当于delete()缓存
     */
	@Cacheable(value="myCache",key="#token")
    public String getToken(String token)
    {
    	return token;
    }
}
